package org.footito.android.ui.activity;

import org.footito.android.R;
import org.footito.android.data.update.UpdateService;
import org.footito.android.tools.Log;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class FootitoPreferenceActity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	public static final String KEY_REFRESHTIME = "refreshtime";

	public static void startActivity(Context context) {
		Intent intent = new Intent(context, FootitoPreferenceActity.class);
		context.startActivity(intent);
	}
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.footitoprefs);

		SharedPreferences prefs = this.getPreferenceManager().getSharedPreferences();
		prefs.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (key != null) {
			if (key.equals(KEY_REFRESHTIME)) {
				try {
					Log.v(KEY_REFRESHTIME + " changed");
					int value = Integer.parseInt(sharedPreferences.getString(key, "0"));
					if (value > 0) {
						UpdateService.scheduleService(this, value);
					} else {
						UpdateService.unScheduleService(this);
					}
				} catch (Exception e) {
					Log.e(e);
				}
			}
		}
	}


}
